
//简易静态资源服务器的搭建

const { log } = require("console");
const fs  = require("fs");
const http = require("http");
const server = http.createServer((request, response) => {
   //得到请求路径
    let {pathname}=new URL(request.url,"http://localhost:8989")
    
    //如果是根路径 / ,改为 /index.html
    if (pathname === "/") {
        pathname = "/index.html";
    }

    //__dirname  获取当前文件所在目录的绝对路径
    console.log(__dirname);// D:\web_code\jsHighClass\es5-6\es6\code3\day11
    const publicDirPath = __dirname + "/public";
    console.log(publicDirPath);// D:\web_code\jsHighClass\es5-6\es6\code3\day11/public
    
    //要加载的资源路径
    const filePath = publicDirPath + pathname;
    console.log(filePath);

    fs.readFile(filePath, (error, data) => {
        if (error) {
            response.end("<h1>404 Not Found</h1>");
        } else {
            response.end(data)
        }
    })
    
})
server.listen("8989", () => {
    console.log("服务器启动:http://localhost:8989/login");
})